<?php
namespace Phone\Model;
use Think\Model;
/**
 * 手机用户管理模型
 */
class UserModel extends Model{
	

	
	public $_validate =array();//申明自动验证
 	/*
	 * 登录判定
	 */
	 public function Login(){
	 	//自动验证
//	 	$this->_validate=array(
//			array('user','require', '用户名不能为空'),
//			array('pw','require','用户密码不能为空')			
//		);
//		//出发自动验证
//		if(!$this->create()){
////			return false;
//		}
		$user = I('post.user');
		$pw = I('post.pw');
		$email = I('post.email');
		//查询数据库判断用户信息
		$userData = M('user')->where("uusername='{$user}'")->find();
		if($userData){ 	//如果存在则比对判断
			//比对用户密码信息
			if($userData['upasswd'] == md5($pw)){
				$token = md5($pw.substr(md5($pw),0,10));
				$data =array(
					'error'    => 0,	
					'username'  => $user,
					'token' 	=>$token,
					'unickname' =>$userData['unickname'],
					'uid'		=>$userData['uid'],
				);
				return $data;
			}else{
				$data =array(
					'error' => 1,
					'message'=> '用户名密码不匹配',		
				);
			}
			
		}else{  //用户名不存在返回错误
			$data =array(
				'error' => 1,
				'message'=> '用户名不存在',		
			);			
			return $data;
		}
	 }
	 
	 
	 /**
	  * 手机端用户注册
	  */
	  public function userReg(){
	  	$user = I('post.user');
		$pw = I('post.pw1');
		$email = I('post.email');
		$bool = M('user')->where("uusername ='{$user}'")->find();
		if($bool){
			$data['error'] = 1;
			$data['message'] = '用户名已存在';
			return $data;			
		}else{
			$data['uusername']=$user;
			$data['upasswd']=md5($pw);
			$data['uemail']=$email;
			M('user')->add($data);
			$data['error'] = 0;
			$data['message']='注册成功';
			return $data;
		}
	  }
 }
